package com.canbot.u05.activity.presidemode;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.canbot.u05.R;
import com.canbot.u05.activity.presidemode.eventbus.AudioEvent;
import com.canbot.u05.bean.guide.MapVoiceAndEyes;
import com.canbot.u05.socket.IpMsgConst;
import com.canbot.u05.utils.Logger;
import com.canbot.u05.utils.OkHttpUtils;
import com.canbot.u05.utils.PathConst;
import com.canbot.u05.utils.SoundManager;
import com.canbot.u05.utils.StatusBarUtil;
import com.canbot.u05.utils.ToastTools;
import com.canbot.u05.view.GuideRecordDialog;

import java.io.File;
import java.util.HashMap;

/**
 * @author zmp
 * 主持人模式语言设置 --导览公用
 * 是否可设置无
 * getBooleanExtra("isEnableNull", false);
 */
public class HostModeCustomModuleActivity extends BaseCompereActivity implements View.OnClickListener {

        private final static String TAG = "HostModeCustomModuleActivity";

        private CheckBox checkBox0, checkBox3;

        private CheckBox checkBox1, checkBox2;

        private Button btnBack;

        private Button btnFinish;

        private ToastTools toastTools;

        private View ivOk;

        private View ivPb;

        private TextView tvLoad;

        private AudioEvent recodeAudio;

        private boolean isEnableNull;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_host_mode_custom_module);
                toastTools = ToastTools.newInstance(this);
                Intent intentResult = getIntent();
                isEnableNull = intentResult.getBooleanExtra("isEnableNull", false);
                type = intentResult.getIntExtra("type", -1);
                audioPath = intentResult.getStringExtra("path");
                content = intentResult.getStringExtra("content");
                setViews();
        }

        private void setViews() {
                View viewById = findViewById(R.id.host_mode_no_choose);
                viewById.setOnClickListener(this);
                findViewById(R.id.host_mode_record).setOnClickListener(this);
                findViewById(R.id.host_mode_rl_audio).setOnClickListener(this);
                findViewById(R.id.host_mode_rl_text_to_speech).setOnClickListener(this);
                checkBox0 = (CheckBox) findViewById(R.id.cbox_host_mode_no_choose);
                checkBox1 = (CheckBox) findViewById(R.id.cbox_host_mode_choose_exist_audio);
                checkBox2 = (CheckBox) findViewById(R.id.cbox_host_mode_text_to_speech);
                checkBox3 = (CheckBox) findViewById(R.id.cbox_host_mode_record);
                btnBack = (Button) findViewById(R.id.host_mode_custom_imageView_back);
                btnFinish = (Button) findViewById(R.id.host_mode_finish_btn);
                btnBack.setOnClickListener(this);
                btnFinish.setOnClickListener(this);
                if (isEnableNull) {
                        viewById.setVisibility(View.VISIBLE);
                }
                else {
                        viewById.setVisibility(View.GONE);
                }
                setItemSelected();

        }

        private void setItemSelected() {
                checkBox0.setChecked(false);
                checkBox1.setChecked(false);
                checkBox2.setChecked(false);
                checkBox3.setChecked(false);
                switch (type) {
                        case AudioEvent.AUDIO_TYPE_AUDIO:
                                checkBox1.setChecked(true);
                                break;
                        case AudioEvent.AUDIO_TYPE_TTS:
                                checkBox2.setChecked(true);
                                break;
                        case AudioEvent.AUDIO_TYPE_RECODE:
                                checkBox3.setChecked(true);
                                break;
                        default:
                                if (isEnableNull) {
                                        checkBox0.setChecked(true);
                                }
                                else {
                                        checkBox2.setChecked(true);
                                }
                                break;
                }
        }

        @Override
        public void onClick(View view) {
                switch (view.getId()) {
                        case R.id.host_mode_custom_imageView_back:
                                finish();
                                overridePendingTransition(R.anim.face_zoom_enter, R.anim.face_zoom_exit);
                                break;
                        case R.id.host_mode_no_choose:  //无
                                checkBox0.setChecked(true);
                                checkBox1.setChecked(false);
                                checkBox2.setChecked(false);
                                checkBox3.setChecked(false);
                                break;

                        case R.id.host_mode_rl_audio:
                                //跳转到选择音频界面
                                startOtherActivity(this, HostModeChooseAudioActivity.class, AudioEvent.AUDIO_TYPE_AUDIO);
                                break;
                        case R.id.host_mode_rl_text_to_speech:
                                //跳转到文字转语音界面
                                startOtherActivity(this, HostModeTextToSpeechActivity.class, AudioEvent.AUDIO_TYPE_TTS);
                                break;
                        case R.id.host_mode_record:   //录音
                                record();
                                break;
                        case R.id.host_mode_finish_btn:
                                //右上角完成按钮
                                if (checkBox0.isChecked() && isEnableNull) {
                                        setResult(RESULT_OK, new Intent());
                                        finish();
                                        return;
                                }
                                if (TextUtils.isEmpty(content)){
                                        toastTools.showText("请先添加语音！");
                                        return;
                                }
                                Intent intent = new Intent();
                                intent.putExtra("type", type);
                                intent.putExtra("path", audioPath);
                                intent.putExtra("content", content);
                                setResult(RESULT_OK, intent);
                                finish();
                                break;

                }
        }

        private int type;  //"tts"  "music"

        private String content;

        private String audioPath;

        private Dialog recoderDilog;

        /**
         * 进入录音
         */
        private void record() {
                if (recoderDilog == null) {
                        //点击语音录入图标
                        recoderDilog = new GuideRecordDialog(this, new GuideRecordDialog.IRecodeListener() {
                                @Override
                                public void finish(String fileName) {
                                        Logger.e("test", "fileName" + fileName);
                                        if (TextUtils.isEmpty(fileName)) {
                                                toastTools.showText("请录入语音!");
                                                return;
                                        }
                                        Logger.d("SpeechSetupActivity", "录音的文件名：" + fileName);
                                        SoundManager.getInstance().playSound(PathConst.ROOT_PATH + PathConst.RECORD_FILE_PATH + fileName, null);
                                        showLoadDialog();
                                        recodeAudio = new AudioEvent(AudioEvent.AUDIO_TYPE_RECODE, PathConst.RECORD_FILE_PATH + fileName, fileName);
                                        sendMsg2Chest(recodeAudio);
                                }

                                @Override
                                public void cancel() {

                                }

                                @Override
                                public void error() {
                                        toastTools.showText("录音失败,请检查权限!");
                                }
                        });
                }
                recoderDilog.show();

        }

        private void sendMsg2Chest(final AudioEvent data) {
                String url = IpMsgConst.HTTP_URL + "guideMapAction";
                HashMap<String, Object> hashMap = new HashMap<>();
                hashMap.put("type", 3 + "");
                hashMap.put("data", JSON.toJSONString(data));
                hashMap.put(data.getMusicName(), new File(PathConst.ROOT_PATH + data.getMusicPath()));
                OkHttpUtils.upLoadFile(url, hashMap, new OkHttpUtils.OkHttpCallBack() {
                        @Override
                        public void onSuccess(String result) {
                                Log.e("TTS2Voice", result);
                                loadDialog.dismiss();
                                toastTools.showText("上传成功!");
                                runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                                type = data.getAudioType();
                                                audioPath = data.getMusicPath();
                                                content = data.getMusicName();
                                                setItemSelected();
                                        }
                                });
                        }

                        @Override
                        public void onFailure() {
                                toastTools.showText("服务器异常！,请重试");
                                loadDialog.dismiss();
                                MapVoiceAndEyes.playMapVoiceAndEyes(MapVoiceAndEyes.voiceEyesList.get(8));
                        }
                });
        }

        private Dialog loadDialog;

        private void showLoadDialog() {
                if (loadDialog == null) {
                        loadDialog = new Dialog(this, R.style.MyDialogStyle);
                        loadDialog.setContentView(R.layout.guide_dialog_load);
                        ivOk = loadDialog.findViewById(R.id.guide_load_iv);
                        ivPb = loadDialog.findViewById(R.id.guide_load_pb);
                        tvLoad = (TextView) loadDialog.findViewById(R.id.guide_load_tv);
                }
                ivPb.setVisibility(View.VISIBLE);
                ivOk.setVisibility(View.INVISIBLE);
                tvLoad.setText("正在上传...");
                loadDialog.setCancelable(false);
                StatusBarUtil.hideVirtualKey(loadDialog);
                loadDialog.show();
        }


        @Override
        protected void onDestroy() {
                super.onDestroy();
                SoundManager.getInstance().resetSound();
        }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                super.onActivityResult(requestCode, resultCode, data);
                if (data == null || resultCode != RESULT_OK) {
                        return;
                }
                if (requestCode == AudioEvent.AUDIO_TYPE_AUDIO) {//音频
                        type = data.getIntExtra("type", -1);
                        audioPath = data.getStringExtra("path");
                        content = data.getStringExtra("content");
                }
                if (requestCode == AudioEvent.AUDIO_TYPE_TTS) {//文字
                        type = data.getIntExtra("type", -1);
                        content = data.getStringExtra("content");
                        audioPath = content;
                        if (TextUtils.isEmpty(content)) {
                                type = -1;
                        }
                }
                setItemSelected();
        }

        /**
         * 跳转界面
         *
         * @param
         */
        protected void startOtherActivity(Context context, Class<?> cls, int code) {
                Intent intent = new Intent(context, cls);
                intent.putExtra("path", audioPath);
                intent.putExtra("content", content);
                intent.putExtra("type", type);
                startActivityForResult(intent, code);
                overridePendingTransition(R.anim.face_zoom_enter, R.anim.face_zoom_exit);
        }
}
